<% unless time_booking.nil? %>
    <td class="checkbox hide-when-print"><%= check_box_tag("ids[]", time_booking.id, false, :id => nil) %></td>
    <td colspan=7>
      <% prefix = "time_booking_edit[]" %>
      <%= fields_for prefix, time_booking do |f| %>
          <%= f.hidden_field(:id) %>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:field_tt_date) %></div>
            <div class="task_form_input">
              <%= hidden_field_tag "time_booking_edit_#{time_booking.id}_valid_dates", [time_booking.time_log.tt_log_date, time_booking.time_log.tt_log_stop_date].join('|') %>
              <%= f.text_field :tt_booking_date, :size => "10", :placeholder => "date", :readonly => time_booking.permission_level < 2, :onchange => "validate_list_inputs('time_booking_edit_#{time_booking.id}');" %>
              <% unless time_booking.permission_level < 2 %>
                  <%= javascript_tag("$(function() { $('#time_booking_edit_#{time_booking.id.to_s}_tt_booking_date').datepicker(locDatepickerOptions); });") %>
              <% end %>
            </div>
          </div>

          <% projects = User.current.memberships.collect(&:project).compact.uniq %>
          <% time_booking.project_id.nil? ? select = nil : select = Project.find(time_booking.project_id) %>
          <% proj_list = "<option value=''>#{ l(:time_tracker_label_none) }</option>" %>
          <% proj_list << "<option value='' disabled='disabled'>---</option>" %>
          <% proj_list = proj_list.html_safe %>
          <% proj_list << project_tree_options_for_select(projects, :selected => select) %>
          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_project) %></div>
            <div class="task_form_input">
              <%= select_tag "time_booking_edit_#{time_booking.id}_project_id_select", proj_list, :onchange => "$('#time_booking_edit_#{time_booking.id}_project_id').val(this.value); validate_list_inputs('time_booking_edit_#{time_booking.id}'); updateBookingActivity('#{User.current.api_key}', 'time_booking_edit_#{time_booking.id}')", :disabled => (time_booking.permission_level < 1 || time_booking.issue_id != l(:time_tracker_label_none)) %>
            </div>
            <%= f.hidden_field :project_id %>
          </div>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_activity) %></div>
            <div class="task_form_input">
              <% activities = get_activities time_booking.project_id %>
              <%= select_tag "time_booking_edit_#{time_booking.id}_activity_id_select", options_from_collection_for_select(activities, :id, :name, time_booking.activity_id),:onchange => "validate_list_inputs('time_booking_edit_#{time_booking.id}');", :name=> "time_booking_edit[#{time_booking.id}][activity_id]", :include_blank => true%>
            </div>
          </div>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_issue) %></div>
            <div class="task_form_input"><%= f.text_field :issue_id, :size => "15", :placeholder => "issue", :onchange => "updateBookingProject('#{User.current.api_key}', 'time_booking_edit_#{time_booking.id}')", :readonly => time_booking.permission_level < 1 %></div>
          </div>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_comments) %></div>
            <div class="task_form_input"><%= f.text_field :comments, :size => 30, :maxlength => 255, :readonly => time_booking.permission_level < 1 %></div>
          </div>

         <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_start_time) + " (min: #{time_booking.time_log.get_formatted_start_time})" %></div>
            <div class="task_form_input">
              <%= hidden_field_tag "time_booking_edit_#{time_booking.id}_min_time", time_booking.time_log.get_formatted_start_time %>
              <%= f.text_field :start_time, :value => time_booking.get_formatted_start_time, :size => "10", :placeholder => "start", :onchange => "updateBookingHours('time_booking_edit_#{time_booking.id}')", :readonly => time_booking.permission_level < 2 %>
            </div>
          </div>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_stop_time) + " (max: #{time_booking.time_log.get_formatted_stop_time})" %></div>
            <div class="task_form_input">
              <%= hidden_field_tag "time_booking_edit_#{time_booking.id}_max_time", time_booking.time_log.get_formatted_stop_time %>
              <%= f.text_field :stop_time, :value => time_booking.get_formatted_stop_time, :size => "10", :placeholder => "stop", :onchange => "updateBookingHours('time_booking_edit_#{time_booking.id}')", :readonly => time_booking.permission_level < 2 %>
            </div>
          </div>

          <div class="task_form_sub_fields">
            <div class="input_hint"><%= l(:time_tracker_label_time_spent) + " (max: #{help.time_dist2string(((time_booking.time_log.bookable_hours + time_booking.hours_spent) * 60).to_i)})" %></div>
            <div class="task_form_input">
              <%= hidden_field_tag "time_booking_edit_#{time_booking.id}_max_spent_time", help.time_dist2string(((time_booking.time_log.bookable_hours + time_booking.hours_spent) * 60).to_i) %>
              <%= f.text_field :spent_time, :value => time_booking.get_formatted_time, :size => "12", :placeholder => "spent time", :onchange => "updateBookingStop('time_booking_edit_#{time_booking.id}')", :readonly => time_booking.permission_level < 2 %>
            </div>
          </div>

          <%= f.submit l(:time_tracker_label_update), :class => "tb_edit_form_update_button" %>
          <%= link_to l(:button_cancel), {:controller => 'time_bookings', :action => 'get_list_entry', :time_booking_id => time_booking.id}, :remote => true %>
      <% end %>
      <%= javascript_tag render :partial => 'edit_form_autocomplete', :locals => {:key => User.current.api_key, :time_booking => time_booking} %>
    </td>

    <%= javascript_tag do %>
        var cols = $("#booking-entry-<%= time_booking.id %>").parents('form:first').find('thead > tr > th').length;
        var myTD = $("#booking-entry-<%= time_booking.id %> > td")[1];
        myTD.setAttribute('colspan', cols - 1);
    <% end %>
<% end %>
